//
//  FuelMainTabViewController.m
//  carcareIOS
//
//  Created by wr on 16/4/14.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "FuelMainTabViewController.h"

#import "FuelMainViewController.h"
#import "BaseWebViewController.h"

#import "SetOilPriceViewController.h"
#import "OilDetailViewController.h"
#import "OilMapDetailViewController.h"

#import "FuelMgr.h"
#import "YearView.h"

typedef NS_ENUM(NSInteger , Segmented_Type)
{
    Segmented_Type_FuelMain = 50,//行程
    Segmented_Type_Webview ,//统计
};

@interface FuelMainTabViewController ()

@property (nonatomic, strong) UISegmentedControl *segmentedControl;

@property (nonatomic, assign) NSInteger segmentType;//切换tab类型

@property (nonatomic , strong) FuelMainViewController * fuelMainVc;

@property (nonatomic , strong) BaseWebViewController * webVC;

@property (nonatomic , strong) UIView * fuelMainView;

@property (nonatomic , strong) UIView * webView;

//是否显示年份下拉菜单
@property (nonatomic) BOOL outAndInBool;
@property (nonatomic , strong) YearView * yearView;
@property (nonatomic , strong) UIImageView * rightUpAndDownImageV;
@property (nonatomic , strong) UILabel * yearTitleL;
@end

@implementation FuelMainTabViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"加油管理";
    
    UIWindow *mainWindow = [UIApplication sharedApplication].delegate.window;
    self.yearView = [[YearView alloc]initOnView:mainWindow];
    [self.yearView setHidden:YES];
    self.outAndInBool = YES;
    
    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"月视图",@"总数据"]];
    self.segmentedControl.frame = CGRectMake(0, 0, 205 * __MAIN_RATIO_375 , 23);
    [self.segmentedControl addTarget:self action:@selector(segmentedChange:) forControlEvents:UIControlEventValueChanged];
    self.segmentedControl.tintColor = [UIColor whiteColor];
    self.segmentedControl.selectedSegmentIndex = 0;
    self.segmentType = Segmented_Type_FuelMain;
    self.navigationItem.titleView = self.segmentedControl;
    
    [self createView];
    
}
- (void)segmentedChange:(UISegmentedControl *)segmented{
    switch (segmented.selectedSegmentIndex) {
        case 0:
            self.segmentType = Segmented_Type_FuelMain;
            [self.fuelMainVc reloadTabelViewData];
            self.outAndInBool = NO;
            [self rightUpAndDown];
            break;
        case 1:
            self.segmentType = Segmented_Type_Webview;
            [self.webVC reloadURL:[FuelMgr sharedInstance].h5Link];

            break;
            
        default:
            break;
    }
    
    [self reloadView];
    
}
- (void)reloadView
{
    [self.fuelMainView setHidden:(self.segmentType == Segmented_Type_FuelMain ? NO : YES)];
    [self.webView setHidden:(self.segmentType == Segmented_Type_Webview ? NO : YES)];
    if(self.segmentType == Segmented_Type_FuelMain && (![FuelMgr sharedInstance].hasDefault))
    {
        [self presentViewController:[UINavigationController navigationControllerWithRootViewController:[[SetOilPriceViewController alloc] initWithTableRefreshType:TYPE_TABLE_REFRESH_NONE]] animated:YES completion:nil];
    }
    if(self.segmentType == Segmented_Type_FuelMain )
    {
        __weak __typeof(self) wself = self;
        [self navSetupRightImageName:@"Nav_Right_Setting" action:^{
            [wself presentViewController:[UINavigationController navigationControllerWithRootViewController:[[SetOilPriceViewController alloc] initWithTableRefreshType:TYPE_TABLE_REFRESH_NONE]] animated:YES completion:nil];
        }];
    }else
    {
        UIView * rightNavView = [[UIView viewWithFrame:CGRectMake(0, 0, 70 * __MAIN_RATIO_375, 35)] resetBackgroundColor:[UIColor clearColor]];
        UIImage * rightImage = ImageNamed(@"calendarDownImage");
        self.rightUpAndDownImageV = [Factory imageViewWithCenter:CGPointMake(rightNavView.size.width - rightImage.size.width / 2, rightNavView.height / 2) image:rightImage onView:rightNavView];
        self.yearTitleL = [Factory labelWithFrame:CGRectMake(0, 0, rightNavView.width - rightImage.size.width - 5, rightNavView.height) font:FONT_DEFAULT_Light(14) text:([FuelMgr sharedInstance].years.count > 0 ? STRING_INT([FuelMgr sharedInstance].choiceYear) : @"年份") textColor:[UIColor whiteColor] onView:rightNavView textAlignment:NSTextAlignmentRight];
         __weak __typeof(self) wself = self;
        [[UIButton buttonEmptyWithFrame:rightNavView.bounds click:^{
            [wself rightUpAndDown];
        }] setupOnView:rightNavView];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightNavView];
    }

}
- (void)rightUpAndDown
{
    [self.yearView setHidden:!self.outAndInBool];
    [self.rightUpAndDownImageV setImage:ImageNamed(self.outAndInBool ? @"calendarUpImage" : @"calendarDownImage")];
    if(self.outAndInBool)
    {
        __weak __typeof(self) wself = self;
        [self.yearView loadDataWithFrameX:__MAIN_WIDTH - VIEW_WIDTH - 5 withFrameY: 50 withArr:[FuelMgr sharedInstance].years withClick:^(NSInteger index)
         {
             [wself.yearView setHidden:YES];
             [wself.rightUpAndDownImageV setImage:ImageNamed(@"calendarDownImage")];
             wself.outAndInBool = !wself.outAndInBool;
             [FuelMgr sharedInstance].choiceYear = [[FuelMgr sharedInstance].years[index] integerValue];
             [wself.yearTitleL setText:([FuelMgr sharedInstance].years.count > 0 ? STRING_INT([FuelMgr sharedInstance].choiceYear) : @"年份")];
             [wself.webVC reloadURL:[FuelMgr sharedInstance].h5Link];

         }];
    }
    
    self.outAndInBool = !self.outAndInBool;
}
- (void)createView
{
    __weak __typeof(self) wself = self;
    self.fuelMainVc = [[FuelMainViewController alloc] init];
    self.fuelMainVc.cellSelectClick = ^(FuelRecordModel * model)
    {
        [[FuelMgr sharedInstance]readNewFuelRecord:model];
        [FuelMgr sharedInstance].choiceRecordModel = model;
        if(model.ISAUTO)
        {
            [wself.navigationController pushViewController:[[[OilMapDetailViewController alloc] initWithTableRefreshType:TYPE_TABLE_REFRESH_NONE ] navSetupBackItemWithIdentification:BACK_ICON_TITLE] animated:YES];
        }else
        {
            [wself.navigationController pushViewController:[[[OilDetailViewController alloc] initWithInputType:TYPE_INPUT_TABLEVIEW withVCType:VC_Type_Look  ] navSetupBackItemWithIdentification:BACK_ICON_TITLE] animated:YES];
        }
        
    };
    self.fuelMainVc.addBtnClick = ^()
    {
        [FuelMgr sharedInstance].choiceRecordModel = nil;
        [wself presentViewController:[UINavigationController navigationControllerWithRootViewController:[[OilDetailViewController alloc] initWithInputType:TYPE_INPUT_TABLEVIEW withVCType:VC_Type_CanEdit_Add]] animated:YES completion:nil];
    };
    self.fuelMainView = self.fuelMainVc.view;
    [self.view addSubview:self.fuelMainView];

    self.webVC = [[[BaseWebViewController alloc]initWithURL:[FuelMgr sharedInstance].h5Link]navSetupBackItemWithIdentification:BACK_ICON];
    self.webVC.isScaleBool = NO;
    self.webView = self.webVC.view;
    [self.view addSubview:self.webView];
    
    [self reloadView];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.fuelMainVc reloadTabelViewData];
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.outAndInBool = NO;
    [self rightUpAndDown];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
